% Source Code Pro for LaTeX
%
% Silke Hofstra, tex@slxh.nl
%
% This file selects either the OTF or the TYPE1 fonts based on
% the LaTeX engine used.
%
% Licenced under the LPPL v1.3 or higher.
%
\NeedsTeXFormat{LaTeX2e}[1994/12/01]
\ProvidesPackage{sourcecodepro}[2018/01/13 v2.7 Adobe's Source Code Pro typeface]

\RequirePackage{ifxetex,ifluatex,xkeyval}

% Switches
\newif\ifsourcecodepro@otf
\newif\ifsourcecodepro@default
\newif\ifsourcecodepro@ttdefault
\newif\ifsourcecodepro@oldstyle
\newif\ifsourcecodepro@black
\newif\ifsourcecodepro@semibold
\newif\ifsourcecodepro@light
\newif\ifsourcecodepro@extralight
\newif\ifsourcecodepro@medium

% Scaling command
\newcommand*{\SourceCodePro@scale}{1}
\newcommand*{\sourcecodepro@ligatures}{}

% Use OpenType fonts for XeTeX and LuaTeX.
\ifxetex
	\sourcecodepro@otftrue
\else
	\ifluatex
		\sourcecodepro@otftrue
	\fi
\fi

\DeclareOptionX{lining}      {\sourcecodepro@oldstylefalse}
\DeclareOptionX{nf}          {\sourcecodepro@oldstylefalse}
\DeclareOptionX{lf}          {\sourcecodepro@oldstylefalse}
\DeclareOptionX{oldstyle}    {\sourcecodepro@oldstyletrue}
\DeclareOptionX{osf}         {\sourcecodepro@oldstyletrue}
\DeclareOptionX{black}       {\sourcecodepro@blacktrue \sourcecodepro@semiboldfalse}
\DeclareOptionX{semibold}    {\sourcecodepro@blackfalse\sourcecodepro@semiboldtrue }
\DeclareOptionX{bold}        {\sourcecodepro@blackfalse\sourcecodepro@semiboldfalse}
\DeclareOptionX{light}       {\sourcecodepro@lighttrue \sourcecodepro@extralightfalse\sourcecodepro@mediumfalse}
\DeclareOptionX{extralight}  {\sourcecodepro@lightfalse\sourcecodepro@extralighttrue \sourcecodepro@mediumfalse}
\DeclareOptionX{regular}     {\sourcecodepro@lightfalse\sourcecodepro@extralightfalse\sourcecodepro@mediumfalse}
\DeclareOptionX{medium}      {\sourcecodepro@lightfalse\sourcecodepro@extralightfalse\sourcecodepro@mediumtrue }
\DeclareOptionX{scale}       {\renewcommand*{\SourceCodePro@scale}{#1}}
\DeclareOptionX{scaled}      {\renewcommand*{\SourceCodePro@scale}{#1}}
\DeclareOptionX{default}     {\sourcecodepro@defaulttrue}
\DeclareOptionX{ttdefault}   {\sourcecodepro@ttdefaulttrue}
\DeclareOptionX{nottdefault} {\sourcecodepro@ttdefaultfalse}
\DeclareOptionX{type1}       {\sourcecodepro@otffalse}
\DeclareOptionX{t1}          {\sourcecodepro@otffalse}
\DeclareOptionX{opentype}    {\sourcecodepro@otftrue}
\DeclareOptionX{otf}         {\sourcecodepro@otftrue}
\DeclareOptionX{ligatures}   {\renewcommand*{\sourcecodepro@ligatures}{#1}}

% Default options
\ExecuteOptionsX{ttdefault,lining,bold,regular}
\ProcessOptionsX\relax

% Load OpenType files
\ifsourcecodepro@otf
	\RequirePackage{fontspec}

	% Create a fixed liningnums command:
	\renewcommand*\liningnums[1]{{\addfontfeature{RawFeature=+lnum;-onum}#1}}

	% Oldstyle/Lining
	\ifsourcecodepro@oldstyle
		\def\sourcecodepro@figurestyle{OldStyle}
	\else
		\def\sourcecodepro@figurestyle{}
	\fi

	% Black/Semibold/Bold
	\ifsourcecodepro@black
		\def\sourcecodepro@boldstyle{Black}
	\else
		\ifsourcecodepro@semibold
			\def\sourcecodepro@boldstyle{Semibold}
		\else
			\def\sourcecodepro@boldstyle{Bold}
		\fi
	\fi

	% Light/ExtraLight/Regular/Medium
	\ifsourcecodepro@light
		\def\sourcecodepro@regstyle{Light}
	\else
		\ifsourcecodepro@extralight
			\def\sourcecodepro@regstyle{ExtraLight}
		\else
			\ifsourcecodepro@medium
				\def\sourcecodepro@regstyle{Medium}
			\else
				\def\sourcecodepro@regstyle{Regular}
			\fi
		\fi
	\fi

	% Shared features
	\defaultfontfeatures{
		Ligatures = \sourcecodepro@ligatures ,
		Numbers   = \sourcecodepro@figurestyle ,
		Scale     = \SourceCodePro@scale ,
		Extension = .otf }

	% Monospace font
	\ifsourcecodepro@ttdefault
		\setmonofont
			[ UprightFont    = *-\sourcecodepro@regstyle ,
			  ItalicFont     = *-\sourcecodepro@regstyle It ,
			  BoldFont       = *-\sourcecodepro@boldstyle ,
			  BoldItalicFont = *-\sourcecodepro@boldstyle It ]
			{SourceCodePro}
	\fi

	% Font families
	% Regular/Bold
	\newfontfamily\sourcecodepro
		[ UprightFont    = *-Regular ,
		  ItalicFont     = *-RegularIt ,
		  BoldFont       = *-Bold ,
		  BoldItalicFont = *-BoldIt ]
		{SourceCodePro}

	% Medium/Bold
	\newfontfamily\sourcecodepromedium
		[ UprightFont    = *-Medium ,
		  ItalicFont     = *-MediumIt ,
		  BoldFont       = *-Bold ,
		  BoldItalicFont = *-BoldIt ]
		{SourceCodePro}

	% Light/Semibold
	\newfontfamily\sourcecodeprolight
		[ UprightFont    = *-Light ,
		  ItalicFont     = *-LightIt ,
		  BoldFont       = *-Semibold ,
		  BoldItalicFont = *-SemiboldIt ]
		{SourceCodePro}

	% ExtraLight/Black
	\newfontfamily\sourcecodeproextreme
		[ UprightFont    = *-ExtraLight ,
		  ItalicFont     = *-ExtraLightIt ,
		  BoldFont       = *-Black ,
		  BoldItalicFont = *-BlackIt ]
		{SourceCodePro}

	% Lining figures
	\newfontfamily\sourcecodeprolf
		[ Numbers        = ,
		  UprightFont    = *-\sourcecodepro@regstyle ,
		  ItalicFont     = *-\sourcecodepro@regstyle It ,
		  BoldFont       = *-\sourcecodepro@boldstyle ,
		  BoldItalicFont = *-\sourcecodepro@boldstyle It ]
		{SourceCodePro}

	% Dont keep all default font features
	\defaultfontfeatures{}

% Load type1 font.
\else
	\RequirePackage[OT1,LY1,T1]{fontenc}
	\RequirePackage{mweights}

	% Oldstyle/Lining
	\ifsourcecodepro@oldstyle
		\def\sourcecodepro@figurestyle{OsF}
	\else
		\def\sourcecodepro@figurestyle{LF}
	\fi

	% Black/Semibold/Bold
	\ifsourcecodepro@black
		\def\bfseries@tt{k}
	\else
		\ifsourcecodepro@semibold
			\def\bfseries@tt{sb}
		\else
			\def\bfseries@tt{b}
		\fi
	\fi

	% Light/ExtraLight/Regular/Medium
	\ifsourcecodepro@light
		\def\mdseries@tt{l}
	\else
		\ifsourcecodepro@extralight
			\def\mdseries@tt{el}
		\else
			\ifsourcecodepro@medium
				\def\mdseries@tt{mb}
			\else
				\def\mdseries@tt{m}
			\fi
		\fi
	\fi

	% Monospace font
	\ifsourcecodepro@ttdefault
		\edef\ttdefault{SourceCodePro-T\sourcecodepro@figurestyle}
	\fi

	% If source code pro is the default font \oldstylenums needs a new definition.
	\ifsourcecodepro@default
		\let\nativeoldstylenums\oldstylenums
		\renewcommand*\oldstylenums[1]{%
			\begingroup
				\ifmmode
					\mathgroup\symletters #1%
				\else
					\fontfamily{SourceCodePro-TOsF}\selectfont #1%
				\fi
			\endgroup
		}
		\edef\seriesdefault{\mdseries@tt}
	\fi
\fi

% Set default font family
\ifsourcecodepro@default
	\edef\familydefault{\ttdefault}
\fi

\endinput
